package com.beagle.njddapp.ui.presenter;

import android.content.Intent;
import android.text.TextUtils;

import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.BaseApplication;
import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.NjOwnerApiService;
import com.beagle.njddapp.base.OilApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.LoginBean;
import com.beagle.njddapp.bean.UserInfoBean;
import com.beagle.njddapp.bean.oil.OilBean;
import com.beagle.njddapp.bean.owner.DIrverOrderBean;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.net.BaseHttpResponse;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.BaseOilHttpResponse;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.activity.BasicUserInfoActivity;
import com.beagle.njddapp.ui.activity.MainActivity;
import com.beagle.njddapp.ui.activity.SplanshActivity;
import com.beagle.njddapp.ui.view.LoginView;
import com.beagle.njddapp.utils.PrefsManager;
import com.beagle.njddapp.utils.Utils;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.LinkedHashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

import static com.beagle.njddapp.core.UserService.ROLE_DIRVER;
import static com.beagle.njddapp.core.UserService.ROLE_OIL;
import static com.beagle.njddapp.core.UserService.ROLE_OWNER;
import static com.beagle.njddapp.core.UserService.TOKEN;

/**
 * @author hxx
 * @date 2021-03-02 22:33
 */
public class LoginPresenter extends BasePresenter<LoginView> {
    private ApiService apiService;
    private NjOwnerApiService njOwnerApiService;
    private OilApiService OilapiService;

    public LoginPresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        apiService = retrofit.create(ApiService.class);
        njOwnerApiService = retrofit.create(NjOwnerApiService.class);
        OilapiService = retrofit.create(OilApiService.class);
    }


    public void getMsg(String phone,String acctionType) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("mobile", phone);
        paramsMap.put("acctionType", acctionType);
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        paramsMap.clear();
        apiService.getMsg(body).compose(RxSchedulers.compose()).subscribe(new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object o) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void login(String phone, String code,String type) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("mobile", phone);
        paramsMap.put("verifyCode", code);
        paramsMap.put("type", type);
        paramsMap.put("loginPassword", "");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        paramsMap.clear();
        apiService.login(Utils.getPhoneVesion(),Utils.getVersionCode(BaseApplication.getInstance().getApplicationContext())+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseHttpResponse<LoginBean>>() {
            @Override
            public void success(BaseHttpResponse<LoginBean> loginBeanBaseHttpResponse) {
                LogUtils.d("login success");
                if (loginBeanBaseHttpResponse != null && loginBeanBaseHttpResponse.data != null) {
                    if ("register".equals(type)){
                        getView().toMian();
                        return;
                    }
                    LoginBean loginBean = loginBeanBaseHttpResponse.data;
                    PrefsManager.getInstance(BaseApplication.getInstance()).putString(TOKEN, loginBean.getAuthorization());
                    UserService.getInstance().setToken(loginBean.getAuthorization());
                    UserService.getInstance().setPhone(phone);
                    if (TextUtils.isEmpty(loginBean.getUserName())){
                        getView().toInfo();
                    }else  if (loginBean.getUserRole()==0){
                        getView().toChooseRole();
                    }else{
                        getUserInfo();
                    }
                }
            }

            @Override
            public void error(int code, String message) {
                LogUtils.d("error");
                getView().onError(message);

            }
        });
    }
    public void register(String phone, String code,String type,String vercode) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("mobile", phone);
        paramsMap.put("verifyCode", vercode);
        paramsMap.put("type", type);
        paramsMap.put("loginPassword", code);
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        paramsMap.clear();
        apiService.login(Utils.getPhoneVesion(),Utils.getVersionCode(BaseApplication.getInstance().getApplicationContext())+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseHttpResponse<LoginBean>>() {
            @Override
            public void success(BaseHttpResponse<LoginBean> loginBeanBaseHttpResponse) {
                LogUtils.d("login success");
                if (loginBeanBaseHttpResponse != null && loginBeanBaseHttpResponse.data != null) {
                    if ("register".equals(type)){
                        getView().toMian();
                        return;
                    }
                    LoginBean loginBean = loginBeanBaseHttpResponse.data;
                    PrefsManager.getInstance(BaseApplication.getInstance()).putString(TOKEN, loginBean.getAuthorization());
                    UserService.getInstance().setToken(loginBean.getAuthorization());
                    UserService.getInstance().setPhone(phone);
                    if (TextUtils.isEmpty(loginBean.getUserName())){
                        getView().toInfo();
                    }else if (loginBean.getUserRole()==0){
                        getView().toChooseRole();
                    }else{
                        getUserInfo();
                    }
                }
            }

            @Override
            public void error(int code, String message) {
                LogUtils.d("error");
                getView().onError(message);

            }
        });
    }
    public void resetPwd(String phone, String code,String pwd) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("mobile", phone);
        paramsMap.put("verifyCode", code);
        paramsMap.put("newPassWord", pwd);
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        paramsMap.clear();
        apiService.resetPwd(body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseHttpResponse<LoginBean>>() {
            @Override
            public void success(BaseHttpResponse<LoginBean> loginBeanBaseHttpResponse) {
                LogUtils.d("login success");
                if (loginBeanBaseHttpResponse != null && loginBeanBaseHttpResponse.data != null) {
                    getView().toMian();
                }else {
                    getView().onError("服务异常，请重试");
                }
            }
            @Override
            public void error(int code, String message) {
                LogUtils.d("error");
                getView().onError(message);

            }
        });
    }


    private void getUserInfo() {
        apiService.getUserInfo().compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseHttpResponse<UserInfoBean>>() {
            @Override
            public void success(BaseHttpResponse<UserInfoBean> userInfoBeanBaseHttpResponse) {
                LogUtils.d("getUserInfo success");
                if (userInfoBeanBaseHttpResponse != null && userInfoBeanBaseHttpResponse.data != null) {
                   UserInfoBean infoBean=userInfoBeanBaseHttpResponse.data;
                    UserService.getInstance().setUserInfoBean(userInfoBeanBaseHttpResponse.data);
                    if (TextUtils.isEmpty(infoBean.getUserName())){
                        getView().toInfo();
                   }else  if (0==infoBean.getUserRole()){
                        getView().toChooseRole();
                    }else{
                        getView().toMian();
                   }
                }else {
                    getView().onError("请重试");
                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }

    public void firstRequest() {
        if (UserService.getInstance().getRole()==ROLE_OWNER){
            Gson gson=new Gson();
            HashMap<String,String> paramsMap=new LinkedHashMap<>();
            String strEntity = gson.toJson(paramsMap);
            RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
            paramsMap.clear();
            njOwnerApiService.getDeviceList(1000+"",1+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse>() {


                @Override
                public void success(BaseOilHttpListResponse baseOilHttpListResponse) {
                    getView().toMian();
                }

                @Override
                public void error(int code, String message) {
                    getView().toMian();

                }
            });
        }else if (UserService.getInstance().getRole()==ROLE_OIL){
            RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),"{}");
            OilapiService.myCard(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse<OilBean>>() {
                @Override
                public void success(BaseOilHttpResponse<OilBean> oilHttpResponse) {
                    getView().toMian();

                }

                @Override
                public void error(int code, String message) {
                    getView().toMian();
                }
            });
        }else {
            Gson gson = new Gson();
            HashMap<String, String> paramsMap = new LinkedHashMap<>();
            paramsMap.put("orderId", "");
            paramsMap.put("orderNum", "");
            paramsMap.put("orderStatus", "");
            String strEntity = gson.toJson(paramsMap);
            RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
            paramsMap.clear();
            njOwnerApiService.queryFindDriverOrderList(1000 + "", 1 + "", body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpListResponse<DIrverOrderBean>>() {
                @Override
                public void success(BaseOilHttpListResponse<DIrverOrderBean> orderBeanBaseOilHttpListResponse) {
                    getView().toMian();
                }

                @Override
                public void error(int code, String message) {
                    getView().toMian();
                }
            });
        }
    }
}
